package com.rsscollector.util;

import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * Created by IntelliJ IDEA.
 * User: Vu Nga
 * Date: Sep 8, 2007
 * Time: 3:22:23 PM
 * To change this template use File | Settings | File Templates.
 */
public class UpdateIPAddressJob implements Job {
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println("Start update ip address");

        HttpClient client = new HttpClient();
        HttpMethod method = new GetMethod("http://pavietnam.net/getip.php");
        // Provide custom retry handler is necessary
        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler(3, false));

        try {
            // Execute the method.
            int statusCode = client.executeMethod(method);
            if (statusCode != HttpStatus.SC_OK) {
                System.err.println("Method failed: " + method.getStatusLine());
            }

            // This is IP Address of internet
            String responseBody = method.getResponseBodyAsString();
            System.out.println(responseBody);


            // Submit new IP address to DYNDNS server
            String domain = null;
            if (responseBody != null && !"".equals(responseBody)) {
                // tranquang.vn
                domain = "tranquang.vn";
                method = new GetMethod("http://support.pavietnam.net/padyn.php?host=@%7cftp%7cmail%7cwww&ip="+responseBody+"&domain="+domain+"&password=traNquanGcnTT");
                // Provide custom retry handler is necessary
                method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                        new DefaultHttpMethodRetryHandler(3, false));
                // Execute the method.
                statusCode = client.executeMethod(method);
                if (statusCode != HttpStatus.SC_OK) {
                    System.err.println(domain + " Method failed: " + method.getStatusLine());
                }
                String status = method.getResponseBodyAsString();
                System.out.println(status);
                // baovn.com.vn
                domain = "baovn.com.vn";
                method = new GetMethod("http://support.pavietnam.net/padyn.php?host=@%7cftp%7cmail%7cwww&ip="+responseBody+"&domain="+domain+"&password=traNquanGcnTT");
                // Provide custom retry handler is necessary
                method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                        new DefaultHttpMethodRetryHandler(3, false));
                // Execute the method.
                statusCode = client.executeMethod(method);
                if (statusCode != HttpStatus.SC_OK) {
                    System.err.println(domain + " Method failed: " + method.getStatusLine());
                }
                status = method.getResponseBodyAsString();
                System.out.println(status);
            }

        } catch (HttpException e) {
            System.err.println("Fatal protocol violation: " + e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            System.err.println("Fatal transport error: " + e.getMessage());
            e.printStackTrace();
        } finally {
            // Release the connection.
            method.releaseConnection();
        }
    }
}
